<template>
    <section>
        <!--工具条-->
        <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
            <el-form :inline="true" :model="filters">
                <el-form-item>
                    <el-input v-model="filters.name" placeholder="关键字查询"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-select v-model="filters.status" placeholder="请选择">
                        <el-option
                                v-for="item in options"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" v-on:click="getCourseKeyWord">查询</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="handleAdd">新增</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" :disabled="releaseButton" @click="handleBatchRelease">上线</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" :disabled="offlineButton" @click="handleBatchOffline">下线</el-button>
                </el-form-item>
            </el-form>
        </el-col>

        <el-table  ref="multipleTable" :data="course" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
            <el-table-column type="selection" width="55">
            </el-table-column>
            <el-table-column type="index" width="60">
            </el-table-column>
            <el-table-column prop="name" label="课程名称" width="120" sortable>
            </el-table-column>
            <el-table-column prop="courseType.name" label="课程类型" width="100" sortable>
            </el-table-column>
            <el-table-column prop="userName" label="创建人" width="100" sortable>
            </el-table-column>
            <el-table-column prop="startTime" label="上线时间" width="180" sortable :formatter="formatStartTime">
            </el-table-column>
            <el-table-column prop="endTime" label="下线时间" width="180" sortable :formatter="formatEndTime">
            </el-table-column>
            <el-table-column prop="status" type="statu" label="状态" min-width="80" sortable>
                <template scope="scope">
                    <span style="color:green" v-if="scope.row.status==1">上线</span>
                    <span style="color:red" v-else>下线</span>
                </template>
            </el-table-column>
            <el-table-column label="操作" width="300" fixed="right">
                <template scope="scope">
                    <el-button type="default" size="small" @click="handleCourseDetail(scope.$index, scope.row)">基本信息</el-button>
                    <el-button type="success" size="small" @click="handleEdit(scope.$index, scope.row)">市场信息</el-button>
                    <el-button type="danger" size="small" @click="handleEdit(scope.$index, scope.row)">资源信息</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!--工具条-->
        <el-col :span="24" class="toolbar">
            <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
            <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="pageSize" :total="total" style="float:right;">
            </el-pagination>
        </el-col>

        <!--编辑界面-->
        <el-dialog title="编辑" v-model="editFormVisible" :close-on-click-modal="false">
            <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
                <el-form-item label="姓名" prop="name">
                    <el-input v-model="editForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="性别">
                    <el-radio-group v-model="editForm.sex">
                        <el-radio class="radio" :label="1">男</el-radio>
                        <el-radio class="radio" :label="0">女</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="年龄">
                    <el-input-number v-model="editForm.age" :min="0" :max="200"></el-input-number>
                </el-form-item>
                <el-form-item label="生日">
                    <el-date-picker type="date" placeholder="选择日期" v-model="editForm.birth"></el-date-picker>
                </el-form-item>
                <el-form-item label="地址">
                    <el-input type="textarea" v-model="editForm.addr"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="editFormVisible = false">取消</el-button>
                <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--新增界面-->
        <el-dialog title="新增" :visible.sync="courseVisible" :close-on-click-modal="false">
            <el-form :model="addForm" label-width="80px" :rules="courseRules" ref="addForm">
                <el-form-item label="课程名称" prop="name">
                    <el-input v-model="addForm.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="适用人群" prop="users">
                    <el-input v-model="addForm.users" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="课程类型">
                    <el-cascader :props="props" style="width: 100%" clearable
                                 v-model="addForm.courseTypeId"
                                 :options="courseTypes"></el-cascader>
                </el-form-item>
                <el-form-item label="课程等级" >
                    <!--<el-input v-model="course.grade" auto-complete="off"></el-input>-->
                    <!--<el-radio v-for="level in levels" v-model="course.grade" :label="level.id">{{level.name}}</el-radio>-->
                    <el-radio v-model="grade" :label="21">初级</el-radio>
                    <el-radio v-model="grade" :label="22">中级</el-radio>
                    <el-radio v-model="grade" :label="23">高级</el-radio>
                </el-form-item>
                <el-form-item label="课程图片">
                    <!--<el-input v-model="course.pic" auto-complete="off"></el-input>-->
                    <el-upload
                            action="http://localhost:1299/services/file/upload"
                            :on-success="handleUploadSuccess"
                            :limit="1"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                </el-form-item>
                <el-form-item label="课程简介">
                    <el-input type="textarea" v-model="addForm.intro" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="课程详情">
                    <!--<el-input type="textarea" v-model="course.description" auto-complete="off"></el-input>-->
                    <quill-editor
                            v-model="addForm.description"
                    ></quill-editor>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="courseVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>

        <!--基本信息展示和修改界面-->
        <el-dialog title="基本信息" :visible.sync="courseDetailVisible" :close-on-click-modal="false">
            <el-form :model="courseDetail" label-width="80px" ref="courseDetail">
                <el-form-item label="课程名称" prop="name">
                    <el-input v-model="courseDetail.name" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="适用人群" prop="users">
                    <el-input v-model="courseDetail.users" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="课程类型">
                    <el-cascader :props="props" style="width: 100%" clearable
                                 v-model="courseDetail.courseTypeId"
                                 :options="courseTypes"></el-cascader>
                </el-form-item>
                <el-form-item label="课程等级" >
                    <!--<el-input v-model="course.grade" auto-complete="off"></el-input>-->
                    <!--<el-radio v-for="level in levels" v-model="course.grade" :label="level.id">{{level.name}}</el-radio>-->
                    <el-radio v-model="grade" :label="21">初级</el-radio>
                    <el-radio v-model="grade" :label="22">中级</el-radio>
                    <el-radio v-model="grade" :label="23">高级</el-radio>
                </el-form-item>
                <el-form-item label="课程图片">
                    <!--<el-input v-model="course.pic" auto-complete="off"></el-input>-->
                    <el-upload
                            action="http://localhost:1299/services/file/upload"
                            :on-success="handleUploadSuccess"
                            :limit="1"
                            list-type="picture">
                        <el-button size="small" type="primary">点击上传</el-button>
                    </el-upload>
                </el-form-item>
                <el-form-item label="课程简介">
                    <el-input type="textarea" v-model="courseDetail.intro" auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="课程详情">
                    <!--<el-input type="textarea" v-model="course.description" auto-complete="off"></el-input>-->
                    <quill-editor
                            v-model="courseDetail.description"
                    ></quill-editor>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click.native="courseDetailVisible = false">取消</el-button>
                <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
            </div>
        </el-dialog>
    </section>
</template>

<script>

    import { quillEditor } from 'vue-quill-editor';
    import 'quill/dist/quill.core.css'
    import 'quill/dist/quill.snow.css'
    import 'quill/dist/quill.bubble.css'

    export default {
        components:{
            quillEditor
        },
        data() {
            return {
                multipleTable:{
                    status:null,
                },
                releaseButton:false,
                offlineButton:false,
                grade:21,
                courseTypes:[],
                props:{
                    label:"name",
                    value:"id"
                },
                options: [{
                    value: 1,
                    label: '上线'
                }, {
                    value: 0,
                    label: '下线'
                }],
                filters: {
                    name: '',
                    status: null
                },
                course: [],
                total: 0,
                page: 1,
                pageSize:10,
                listLoading: false,
                sels: [],//列表选中列

                editFormVisible: false,//编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [
                        { required: true, message: '请输入姓名', trigger: 'blur' }
                    ]
                },
                //编辑界面数据
                editForm: {
                    name: '',
                    users: '',
                    CourseTypeId: null,
                    grade: null,
                    intro: '',
                    description: '',
                    pic: ''
                },

                courseVisible: false,//新增界面是否显示
                addLoading: false,
                courseRules: {
                    name: [
                        { required: true, message: '请输入课程名称', trigger: 'blur' }
                    ]
                },
                //新增界面数据
                addForm: {
                    name: '',
                    users: '',
                    CourseTypeId: null,
                    grade: null,
                    intro: '',
                    description: '',
                    pic: ''
                },
                //修基本信息展示和修改界面是否显示
                courseDetailVisible:false,
                //修基本信息展示和修改界面的数据
                courseDetail: {
                    name: '',
                    users: '',
                    CourseTypeId: null,
                    grade: null,
                    intro: '',
                    description: '',
                    pic: ''
                }

            }
        },
        methods: {
            formatStartTime(row, column){
                return this.formatTime(row.startTime)
            },
            formatEndTime(row, column){
                return this.formatTime(row.endTime)
            },
            //时间显示转换
            formatTime: function (time) {
                if(!time){
                    return "";
                }
                let date = new Date(time);
                let year = date.getFullYear();
                let mounth = date.getMonth()+1;
                let day = date.getDate();//getDay是一周中的第几天,getDate是一个月中的第几天
                let hours = date.getHours();
                let minutes = date.getMinutes();
                let seconds = date.getSeconds();
                return year+"-"+this.formatNum(mounth)+"-"+this.formatNum(day)
                    +" "+this.formatNum(hours)+":"+this.formatNum(minutes)+":"+this.formatNum(seconds);
            },
            formatNum(num){
                if(num<10){
                    return "0"+num;
                }
                return num;
            },
            handleCurrentChange(val) {
                this.page = val;
                this.getCourses();
            },
            handleCurrentChange(val) {
                this.page = val;
                this.getCourse();
            },
            //关键字高级查询
            getCourseKeyWord(){
                this.page=1;
                this.getCourse()
            },
            //获取用户列表
            getCourse() {
                let para = {
                    pageNum: this.page,
                    pageSize:this.pageSize,
                    keyword:this.filters.name,
                    status:this.filters.status
                };
                this.listLoading = true;

                this.$http.post("/course/course/page",para).then(res=>{
                    this.course=res.data.rows;
                    this.total=res.data.total
                    this.listLoading = false;
                })
                /*//NProgress.start();
                getCourseListPage(para).then((res) => {
                    this.total = res.data.total;
                    this.course = res.data.course;
                    this.listLoading = false;
                    //NProgress.done();
                });*/
            },
            handleBatchRelease(){
                console.log(this.sels)
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认上线选中的全部课程吗吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let para = { ids: ids };
                    console.log(para)
                    this.$http.post("/course/course/batchRelease",para).then(res=>{
                        if (res.data.success){
                            this.listLoading=false;
                            this.getCourse();
                        }else {
                            this.$message({
                                message: '下线失败',
                                type: 'error'
                            });
                        }
                    })

                })
            },
            handleBatchOffline(){
                var ids = this.sels.map(item => item.id);
                this.$confirm('确认下线选中的全部课程吗吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    let para = { ids: ids };
                    console.log(para)
                    this.$http.post("/course/course/batchOffline",para).then(res=>{
                        if (res.data.success){
                            this.listLoading=false;
                            this.getCourse();
                        }else {
                            this.$message({
                                message: '下线失败',
                                type: 'error'
                            });
                        }
                    })


                })
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除该记录吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = { id: row.id };
                    removeCourse(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getCourse();
                    });
                }).catch(() => {

                });
            },
            //显示修基本信息展示和修改界面
            handleCourseDetail: function (index, row) {
                this.courseDetailVisible = true;
                //回显课程等级
                this.$http.get("/course/course/"+row.id).then(res=>{
                    this.$http.get("/system/systemdictionaryItem/"+res.data.grade).then(res=>{
                        this.grade=res.data.id
                    })
                });
                //回显课程简介和详情
                this.$http.get("/course/courseDetail/getDetailByCourseId/"+row.id).then(res=>{
                    this.courseDetail.intro=res.data.intro;
                    this.courseDetail.description=res.data.description;
                })
                this.courseDetail = Object.assign({}, row);
            },
            //编辑
            editSubmit: function () {

            },
            //显示新增界面
            handleAdd: function () {
                this.courseVisible = true;
                this.addForm = {
                    name: '',
                    users: '',
                    CourseTypeId: null,
                    grade: null,
                    intro: '',
                    description: '',
                    pic: ''
                };
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);
                            para.grade = this.grade;
                            //cascader 级联选择器选中的结果是一个数组，我们只需要提交最后一级的类型
                            para.courseTypeId = this.addForm.courseTypeId[this.addForm.courseTypeId.length-1];
                            this.$http.post("/course/course/add",para)
                                .then(res=>{
                                    this.addLoading = false;
                                    let {success,message} = res.data;
                                    if(success){
                                        this.$message({
                                            message: '提交成功',
                                            type: 'success'
                                        });
                                        this.$refs['addForm'].resetFields();
                                        this.courseVisible = false;
                                        this.getCourse();
                                    }else{
                                        this.$message({
                                            message: message,
                                            type: 'error'
                                        });
                                    }
                                })
                        });
                    }
                });
            },
            // 递归判断列表，把最后的children设为undefined
            getTreeData(data){
                for(var i=0;i<data.length;i++){
                    if(data[i].children.length<1){
                        // children若为空数组，则将children设为undefined
                        data[i].children=undefined;
                    }else {
                        // children若不为空数组，则继续 递归调用 本方法
                        this.getTreeData(data[i].children);
                    }
                }
                return data;
            },
            //加载课程类型
            getCourseTypes(){
                this.$http.get("/course/courseType/getCourseTypeTree")
                    .then(res=>{
                        this.courseTypes = this.getTreeData(res.data.resultObj);
                    })
            },
            //课程图片上传成功
            handleUploadSuccess(response, file, fileList){
                let {success,message,resultObj} = response;
                if(success){
                    this.addForm.pic = resultObj;
                    this.$message({
                        message: "上传成功!",
                        type: 'success'
                    });
                }else{
                    this.$message({
                        message: "上传失败!",
                        type: 'error'
                    });
                }
            },
            //查询等级
            getLevels(){
                this.$http.get("/system/systemdictionaryItem/getCourseLevel")
                    .then(res=>{
                        this.levels = res.data;
                        //初始化couse.grade的值
                        this.grade = this.levels[0].id;
                    })
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = { ids: ids };
                    batchRemoveCourse(para).then((res) => {
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getCourse();
                    });
                }).catch(() => {

                });
            }
        },
        mounted() {
            this.getCourse();
            this.getCourseTypes();
            this.getLevels();
        }
    }

</script>

<style scoped>

</style>